//
//  HNSH_SearchGoodsNavView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/24.
//

#import "HNSH_SearchGoodsNavView.h"

@interface HNSH_SearchGoodsNavView ()<UITextFieldDelegate>
/** 返回按钮 */
@property (nonatomic, strong)UIButton * backBtn;
/** 搜索框灰色背景 */
@property (nonatomic, strong)UIView * grayBgView;
/** 搜索图片 */
@property (nonatomic, strong)UIImageView * searchImgView;
/** 输入框 */
@property (nonatomic, strong)UITextField * textField;
/* searchLabel */
@property (nonatomic, strong) UILabel *searchLabel;

@end

@implementation HNSH_SearchGoodsNavView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor appWhiteLabelColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
    imgV.image = [UIImage imageNamed:@"home_search_nav_bg"];
    [self addSubview:imgV];
    [self sendSubviewToBack:imgV];
    
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self addSubview:self.backBtn];
    [self addSubview:self.grayBgView];
    [self.grayBgView addSubview:self.searchImgView];
    [self.grayBgView addSubview:self.textField];
    [self.grayBgView addSubview:self.searchLabel];
}

- (void)addAllLayout{
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(7);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(kStatusBarHeight);
    }];
    [self.grayBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backBtn.mas_right).offset(8);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(32);
        make.top.mas_equalTo(kStatusBarHeight + (44 - 32) / 2);
    }];
    [self.searchImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.width.height.mas_equalTo(16);
        make.centerY.mas_equalTo(self.grayBgView);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.searchImgView.mas_right).offset(5);
        make.top.height.right.mas_equalTo(self.grayBgView);
    }];
}

//MARK: Myself Methods
- (void)backBtnAction{
    if (self.backBlock) {
        self.backBlock();
    }
}

- (void)searchBtnAction{
    if (self.textField.text && self.textField.text.length) {
        if (self.searchBlock) {
            self.searchBlock(self.textField.text);
        }
    }
}

- (void)setClearField:(BOOL)clearField {
    if (clearField) {
        self.textField.text = @"";
    }
}

//MARK: UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text && textField.text.length) {
        if (self.searchBlock) {
            self.searchBlock(textField.text);
        }
    }
    return YES;
}

#pragma mark - Getter Methods
- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"base_nav_back_black"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIView *)grayBgView{
    if (!_grayBgView) {
        _grayBgView = [UIView new];
        _grayBgView.backgroundColor = [UIColor appWhiteLabelColor];
        _grayBgView.layer.cornerRadius = 16;
        _grayBgView.layer.masksToBounds = YES;
    }
    return _grayBgView;
}

- (UIImageView *)searchImgView{
    if (!_searchImgView) {
        _searchImgView = [UIImageView new];
        _searchImgView.image = [UIImage imageNamed:@"base_nav_search"];
    }
    return _searchImgView;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [UITextField new];
        _textField.backgroundColor = [UIColor clearColor];
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.font = [UIFont systemFontOfSize:14];
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.enablesReturnKeyAutomatically = YES;
        _textField.placeholder = @"请输入商品或分类";
        NSMutableDictionary * attDic = [@{NSForegroundColorAttributeName:[UIColor appGrayLabelColor], NSFontAttributeName:_textField.font} mutableCopy];
        NSMutableAttributedString * attPlace = [[NSMutableAttributedString alloc] initWithString:_textField.placeholder attributes:attDic];
        _textField.attributedPlaceholder = attPlace;
        _textField.delegate = self;
    }
    return _textField;
}

- (UILabel *)searchLabel{
    if (!_searchLabel) {
        _searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth-64-60, 3, 56, 30)];
        _searchLabel.textColor = [UIColor appWhiteLabelColor];
        _searchLabel.font = [UIFont systemFontOfSize:14];
        _searchLabel.text = @"搜索";
        _searchLabel.textAlignment = NSTextAlignmentCenter;
        _searchLabel.backgroundColor = [UIColor due_colorGradientChangeWithSize:CGSizeMake(56, 30) direction:DUEGradientChangeDirectionLevel startColor:[UIColor colorWithHexString:@"#3EA700"] endColor:[UIColor colorWithHexString:@"#6AD335"]];;
        _searchLabel.layer.cornerRadius = 15.0f;
        _searchLabel.layer.masksToBounds = YES;
        
        __weak __typeof(&*self)weakSelf = self;
        _searchLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            if (weakSelf.searchBlock) {
                weakSelf.searchBlock(weakSelf.textField.text);
            }
        }];
        [_searchLabel addGestureRecognizer:tap];
    }
    return _searchLabel;
}

@end
